package gold.digger;

import java.util.Stack;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC150 {

    public int evalRPN(String[] tokens) {
        if (null == tokens || tokens.length <= 0) {
            return 0;
        }

        Stack<Integer> stack = new Stack<>();
        long res = 0;
        for (int i = 0; i < tokens.length; i++) {
            String curStr = tokens[i];
            if ("+".equals(curStr) || "-".equals(curStr) || "*".equals(curStr) || "/".equals(curStr)) {
                int right = stack.pop();
                int left = stack.pop();

                switch (curStr) {
                    case "+":
                        stack.push(left + right);
                        break;
                    case "-":
                        stack.push(left - right);
                        break;
                    case "*":
                        stack.push(left * right);
                        break;
                    case "/":
                        stack.push(left / right);
                        break;
                }
            } else {
                stack.push(Integer.valueOf(curStr));
            }
        }

        return stack.peek();
    }


    public static void main(String[] args) {
        LC150 an = new LC150();
        String[] input = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
        System.out.println(an.evalRPN(input));
    }
}
